//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Module Name:  

        ist.cpp

Abstract:

       IST framework.

--*/
#include <windows.h>
#include <nkintr.h> // needed for SYSINTR_NOP
#include "monahans_base_regs.h"

#include "args.h"
#include "ioctl_cfg.h"

#include "ist.hpp"

CIST::CIST(LPCTSTR pszActiveKey, DWORD dwISTTimeout, DWORD dwTerminationTimeout)
:   CMiniThread(0,TRUE)
,   m_Reg(pszActiveKey)
,   m_ISTTimeout(dwISTTimeout)
,   m_TerminationTimeout(dwTerminationTimeout)
{
    m_fAllocatedSysIntr = FALSE;
    m_hIsrHandler = NULL;
    m_hISTEvent = NULL;
    m_fIntInitialized = FALSE;
    m_dwIrq = IRQ_UNSPECIFIED;
    m_dwSysIntr= SYSINTR_UNDEFINED;
    BSP_ARGS args;
    DWORD size;
      		//Get Eboot BSP argument to check if use USB 1.1 as default client
    if(KernelIoControl(IOCTL_GET_BSP_ARGS,NULL,0,&args,sizeof(args),&size))
    {
        if(args.kitl.devLoc.PhysicalLoc == (PVOID)MONAHANS_BASE_REG_PA_USB2H)
        {
          RETAILMSG(1, (_T("EBOOT Config use USB 2.0 as KITL \r\n")));
          return ;
        }
    }

    DDKISRINFO ddi;
    if (m_Reg.IsKeyOpened() && m_Reg.GetIsrInfo(&ddi)==ERROR_SUCCESS ) {
        if (ddi.dwIrq!=IRQ_UNSPECIFIED ) {
            if (ddi.dwSysintr == SYSINTR_NOP) 
            { // We need allocate SysIntr for this IRQ.
                BOOL RetVal = KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &ddi.dwIrq, sizeof(ddi.dwIrq), &ddi.dwSysintr, sizeof(ddi.dwSysintr), NULL);
                if (!RetVal || ddi.dwSysintr == SYSINTR_UNDEFINED) {
                    ddi.dwSysintr = SYSINTR_NOP;
                }
                else {
                    m_fAllocatedSysIntr = TRUE ;
                }
            }
        }
        if (ddi.dwIrq!=IRQ_UNSPECIFIED && ddi.dwSysintr != SYSINTR_NOP && ddi.szIsrDll[0] != 0 && ddi.szIsrHandler[0] !=0 ) { // Time for IISR
            m_hIsrHandler = LoadIntChainHandler(ddi.szIsrDll, ddi.szIsrHandler, (BYTE)ddi.dwIrq);
        }
        m_dwIrq = ddi.dwIrq;
        m_dwSysIntr = ddi.dwSysintr ;
    }
    RETAILMSG(1, (L"OTG installed ISTR %d, %d\r\n", m_dwIrq, m_dwSysIntr));
}
BOOL CIST::Init()
{
    return (m_dwSysIntr != SYSINTR_NOP);
}
BOOL  CIST::IntChainHandlerIoControl(DWORD dwIoContro, PVOID pInPtr, DWORD dwInSize, PVOID pOutPtr, DWORD dwOutSize,PDWORD pdwActualWrite)
{
    BOOL fReturn = (m_hIsrHandler?
        (::KernelLibIoControl(m_hIsrHandler,dwIoContro,pInPtr,dwInSize,pOutPtr,dwOutSize,pdwActualWrite)) :
        TRUE);
    ASSERT(fReturn);
    return fReturn;
}

BOOL CIST::IntializeInterrupt(  LPVOID pvData,  DWORD cbData ) 
{
    if (m_dwSysIntr != SYSINTR_NOP && !m_hISTEvent && !m_fIntInitialized) {
        m_hISTEvent= CreateEvent(0,FALSE,FALSE,NULL);
        if (m_hISTEvent) {
            m_fIntInitialized = InterruptInitialize(m_dwSysIntr,m_hISTEvent,pvData, cbData );
        }
    }
    if (m_fIntInitialized) {
        ThreadStart();
    }
    return m_fIntInitialized;
}
CIST::~CIST()
{
    m_bTerminated=TRUE;
    ThreadStart();
    if (m_hISTEvent) {
        SetEvent(m_hISTEvent);
        BOOL fTerminated = ThreadTerminated(m_TerminationTimeout);
        ASSERT(fTerminated) ;
    };
    if (m_fIntInitialized) {
        InterruptDisable(m_dwSysIntr);
    }
    if (m_hISTEvent)
        CloseHandle(m_hISTEvent);
    
    if (m_hIsrHandler) {
        FreeIntChainHandler(m_hIsrHandler);
    }
    if (m_fAllocatedSysIntr && m_dwSysIntr != SYSINTR_NOP) {
        BOOL fRetVal = KernelIoControl( IOCTL_HAL_RELEASE_SYSINTR, &m_dwSysIntr, sizeof( m_dwSysIntr ), NULL, 0, NULL );
    }
}
DWORD   CIST::ThreadRun()
{
    if (!m_bTerminated && m_dwSysIntr!= SYSINTR_NOP && m_hISTEvent!=NULL) {
        BOOL fRet= TRUE;
        while (!m_bTerminated && fRet ) {
            switch (WaitForSingleObject(m_hISTEvent,m_ISTTimeout)){
            case WAIT_OBJECT_0:
                fRet = ISTProcess();
                ASSERT(fRet);
                InterruptDone(m_dwSysIntr);
                break;
            case WAIT_TIMEOUT:
                fRet = ISTTimeout();
                ASSERT(fRet);
				InterruptDone(m_dwSysIntr);
                break;
            default:
                ASSERT(FALSE);
                fRet = FALSE;
                break;
            }
        }
    }
    return 0;
}

